module.exports = (app) => {
    const express = require("express")
    const router = express.Router()
    const mongoose = require("mongoose")
    const Advertise = mongoose.model("Advertise")
    const Category = mongoose.model("Category")
    const Article = mongoose.model("Article")
    const Hero = mongoose.model("Hero")
    const advtiseMap = {
        home: "首页"
    }

    // 根据advType获取广告列表
    router.get("/advertise/list/:type", async (req, res) => {
        const { type } = req.params
        // 调用Model.find()查找文档
        const result = await Advertise.find({
            name: advtiseMap[type]
        })

        // 返回json数据
        res.json({
            code: 1,
            data: result
        })

    })

    // 录入新闻列表数据
    router.get("/news/init", async (req, res) => {
        const parent = await Category.findOne({ name: "新闻分类" })
        const cats = await Category.find().where({
            parent: parent
        }).lean()
        const newsTitles = ["嫦娥皮肤创意赏析第四期，又是被美到的一天~", "重磅合作华为，王者荣耀开启合作新篇章", "嫦娥皮肤设计创意赏析第二期，女神新装抢先看！", "【获奖公示】设计、视频、音乐赛道获奖名单——王者荣耀风物志共创大赛", "如果有一个可以跟王者策划零距离“对线”交流的机会，你会……", "12月26日体验服停机更新公告", "12月25日全服不停机更新公告", "12月24日体验服不停机更新公告", "12月24日体验服停机更新公告", "12月23日净化游戏环境声明及处罚公告", "恭喜DYG夺得KPL秋季赛冠军！冠军回馈来袭，双旦福利开启！", "参与活动免费解锁KPL限定皮肤个人专属购买6折特权", "【手Q独家】好友助力赢现金好礼", "【看KPL总决赛 赢豪华大礼】活动公告", "【手Q独家】商城限时免单", "前瞻丨全国大赛冠军之争 昔日冠军守擂佛山之巅 破刃之师欲夺至高荣誉", "25日战报：虎牙XYG势如破竹，成都TG攻守兼备，全国大赛四强队伍诞生", "全国大赛半决赛开启，今日猫神&amp;佛山GK正式登场解说！", "【战报】乘胜追击！成都TG 3:1 上海9EZ 拿下最后一张全国四强入场券！", "【战报】掌控全场！虎牙XYG 3:0 战胜无锡YC 晋级全国四强！"]
        const newsList = newsTitles.map(title => {
            const randomCats = cats.slice(0).sort((a, b) => Math.random() - 0.5)
            return {
                title: title,
                categories: randomCats.slice(0, 2)
            }
        })

        await Article.deleteMany({})
        await Article.insertMany(newsList)
        res.send(newsList)
    })

    // 获取新闻列表
    router.get("/news/list", async (req, res) => {
        /**
         * 第一种方式：多级填充
         * const result = await Category.findOne({name: "新闻分类")
         * .populate({
         *   path: "children",
         *   populate: { path: "articles" }
         * })
         */

        /**
         * 第二种方式：在分类表中查找父分类为新闻分类的所有分类在关联查找articles虚拟字段
         * const parent = await Category.findOne({ name: "新闻分类" })
         * const conditions = { parent: parent } // 查询条件
         * const populate = { path: "articles" } // 关联参数
         * const options = { lean: true } // 查询参数
         * const result = await Category.find(conditions, null, options).populate(populate) // 调用Model.find()查找文档
         */
        
        // 第三种方式为聚合查询
        const parent = await Category.findOne({ name: "新闻分类" })
        const cats = await Category.aggregate([
            { $match: { parent: parent._id } }, // 过滤数据
            { // 关联查询
                $lookup: {
                    from: "articles",
                    localField: "_id",
                    foreignField: "categories",
                    as: "newsList"
                }
            },
            { // 修改字段只需5条
                $addFields: {
                    newsList: { $slice: ["$newsList", 5] }
                }
            }
        ])

        // 添加热门分类
        const subCats = cats.map(v => v._id)
        cats.unshift({
            name: "热门",
            newsList: await Article.find({ categories: { $in: subCats } })
            .populate("categories").limit(5).lean()
        })

        cats.map(cat => {
            cat.newsList.map(news => {
                news.categoryName = cat.name === "热门" ? news.categories[0].name : cat.name
                return news
            })
            return cat
        })

        // 返回json数据
        res.json({
            code: 1,
            data: cats,
        })
    })

    // 获取文章详情
    router.get("/articles/:id", async (req, res) => {
        const { id } = req.params
        // 返回的是js对象不是mongoose文档
        const result = await Article.findById(id, null, { lean: true })

        const cats = result ? result.categories : []
        const relative = await Article.find({ categories: { $in: cats } }).limit(2)
        res.json({
            code: 1,
            data: {
                ...result,
                relative
            }
        })
    })

    //录入英雄列表数据
    router.get("/heroes/init", async (req, res) => {
        await Hero.deleteMany({})
        const cats = [{"categoryName":"热门","heroerList":[{"name":"鲁班七号","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/112/112.jpg"},{"name":"安琪拉","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/142/142.jpg"},{"name":"后羿","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/169/169.jpg"},{"name":"妲己","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/109/109.jpg"},{"name":"孙悟空","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/167/167.jpg"},{"name":"铠","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg"},{"name":"亚瑟","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/166/166.jpg"},{"name":"瑶","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/505/505.jpg"},{"name":"吕布","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/123/123.jpg"},{"name":"孙尚香","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/111/111.jpg"}]},{"categoryName":"战士","heroerList":[{"name":"赵云","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/107/107.jpg"},{"name":"墨子","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/108/108.jpg"},{"name":"钟无艳","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/117/117.jpg"},{"name":"吕布","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/123/123.jpg"},{"name":"夏侯惇","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/126/126.jpg"},{"name":"曹操","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/128/128.jpg"},{"name":"典韦","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/129/129.jpg"},{"name":"宫本武藏","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/130/130.jpg"},{"name":"达摩","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/134/134.jpg"},{"name":"老夫子","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/139/139.jpg"},{"name":"关羽","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/140/140.jpg"},{"name":"程咬金","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/144/144.jpg"},{"name":"露娜","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/146/146.jpg"},{"name":"花木兰","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/154/154.jpg"},{"name":"橘右京","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/163/163.jpg"},{"name":"亚瑟","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/166/166.jpg"},{"name":"孙悟空","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/167/167.jpg"},{"name":"刘备","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/170/170.jpg"},{"name":"杨戬","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/178/178.jpg"},{"name":"雅典娜","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/183/183.jpg"},{"name":"哪吒","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/180/180.jpg"},{"name":"铠","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg"},{"name":"苏烈","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg"},{"name":"梦奇","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/198/198.jpg"},{"name":"裴擒虎","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/502/502.jpg"},{"name":"狂铁","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/503/503.jpg"},{"name":"孙策","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/510/510.jpg"},{"name":"李信","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/507/507.jpg"},{"name":"盘古","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/529/529.jpg"},{"name":"云中君","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/506/506.jpg"},{"name":"曜","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/522/522.jpg"},{"name":"马超","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/518/518.jpg"},{"name":"蒙恬","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/527/527.jpg"},{"name":"夏洛特","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/536/536.jpg"}]},{"categoryName":"法师","heroerList":[{"name":"小乔","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/106/106.jpg"},{"name":"墨子","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/108/108.jpg"},{"name":"妲己","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/109/109.jpg"},{"name":"嬴政","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/110/110.jpg"},{"name":"高渐离","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/115/115.jpg"},{"name":"孙膑","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/118/118.jpg"},{"name":"扁鹊","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/119/119.jpg"},{"name":"芈月","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/121/121.jpg"},{"name":"周瑜","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/124/124.jpg"},{"name":"甄姬","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/127/127.jpg"},{"name":"武则天","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/136/136.jpg"},{"name":"貂蝉","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/141/141.jpg"},{"name":"安琪拉","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/142/142.jpg"},{"name":"露娜","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/146/146.jpg"},{"name":"姜子牙","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/148/148.jpg"},{"name":"王昭君","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/152/152.jpg"},{"name":"张良","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/156/156.jpg"},{"name":"不知火舞","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/157/157.jpg"},{"name":"钟馗","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/175/175.jpg"},{"name":"诸葛亮","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/190/190.jpg"},{"name":"干将莫邪","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/182/182.jpg"},{"name":"女娲","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/179/179.jpg"},{"name":"杨玉环","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/176/176.jpg"},{"name":"弈星","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/197/197.jpg"},{"name":"米莱狄","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/504/504.jpg"},{"name":"司马懿","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/137/137.jpg"},{"name":"沈梦溪","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/312/312.jpg"},{"name":"上官婉儿","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/513/513.jpg"},{"name":"嫦娥","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/515/515.jpg"},{"name":"西施","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/523/523.jpg"}]},{"categoryName":"坦克","heroerList":[{"name":"廉颇","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/105/105.jpg"},{"name":"庄周","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/113/113.jpg"},{"name":"刘禅","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/114/114.jpg"},{"name":"钟无艳","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/117/117.jpg"},{"name":"白起","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/120/120.jpg"},{"name":"芈月","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/121/121.jpg"},{"name":"吕布","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/123/123.jpg"},{"name":"夏侯惇","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/126/126.jpg"},{"name":"达摩","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/134/134.jpg"},{"name":"项羽","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/135/135.jpg"},{"name":"程咬金","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/144/144.jpg"},{"name":"刘邦","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/149/149.jpg"},{"name":"亚瑟","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/166/166.jpg"},{"name":"牛魔","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/168/168.jpg"},{"name":"张飞","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/171/171.jpg"},{"name":"太乙真人","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/186/186.jpg"},{"name":"东皇太一","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/187/187.jpg"},{"name":"铠","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg"},{"name":"苏烈","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg"},{"name":"梦奇","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/198/198.jpg"},{"name":"孙策","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/510/510.jpg"},{"name":"盾山","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/509/509.jpg"},{"name":"嫦娥","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/515/515.jpg"},{"name":"猪八戒","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/511/511.jpg"},{"name":"蒙恬","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/527/527.jpg"},{"name":"阿古朵","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/533/533.jpg"}]},{"categoryName":"刺客","heroerList":[{"name":"赵云","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/107/107.jpg"},{"name":"阿轲","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/116/116.jpg"},{"name":"李白","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/131/131.jpg"},{"name":"貂蝉","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/141/141.jpg"},{"name":"韩信","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/150/150.jpg"},{"name":"兰陵王","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/153/153.jpg"},{"name":"花木兰","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/154/154.jpg"},{"name":"不知火舞","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/157/157.jpg"},{"name":"娜可露露","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/162/162.jpg"},{"name":"橘右京","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/163/163.jpg"},{"name":"孙悟空","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/167/167.jpg"},{"name":"百里守约","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg"},{"name":"百里玄策","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/195/195.jpg"},{"name":"裴擒虎","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/502/502.jpg"},{"name":"元歌","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/125/125.jpg"},{"name":"司马懿","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/137/137.jpg"},{"name":"上官婉儿","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/513/513.jpg"},{"name":"云中君","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/506/506.jpg"},{"name":"马超","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/518/518.jpg"},{"name":"镜","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/531/531.jpg"},{"name":"澜","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/528/528.jpg"}]},{"categoryName":"射手","heroerList":[{"name":"孙尚香","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/111/111.jpg"},{"name":"鲁班七号","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/112/112.jpg"},{"name":"马可波罗","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/132/132.jpg"},{"name":"狄仁杰","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/133/133.jpg"},{"name":"后羿","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/169/169.jpg"},{"name":"李元芳","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/173/173.jpg"},{"name":"虞姬","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/174/174.jpg"},{"name":"成吉思汗","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/177/177.jpg"},{"name":"黄忠","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/192/192.jpg"},{"name":"百里守约","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg"},{"name":"公孙离","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/199/199.jpg"},{"name":"伽罗","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/508/508.jpg"},{"name":"蒙犽","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/524/524.jpg"}]},{"categoryName":"辅助","heroerList":[{"name":"庄周","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/113/113.jpg"},{"name":"刘禅","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/114/114.jpg"},{"name":"孙膑","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/118/118.jpg"},{"name":"姜子牙","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/148/148.jpg"},{"name":"牛魔","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/168/168.jpg"},{"name":"张飞","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/171/171.jpg"},{"name":"钟馗","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/175/175.jpg"},{"name":"蔡文姬","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/184/184.jpg"},{"name":"太乙真人","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/186/186.jpg"},{"name":"大乔","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/191/191.jpg"},{"name":"东皇太一","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/187/187.jpg"},{"name":"鬼谷子","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/189/189.jpg"},{"name":"明世隐","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/501/501.jpg"},{"name":"杨玉环","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/176/176.jpg"},{"name":"盾山","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/509/509.jpg"},{"name":"瑶","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/505/505.jpg"},{"name":"鲁班大师","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/525/525.jpg"},{"name":"阿古朵","icon":"https://game.gtimg.cn/images/yxzj/img201606/heroimg/533/533.jpg"}]}]
        let heroesList = []
        for (let cat of cats) {
            const category = await Category.findOne({name: cat.categoryName})
            if (category) {
                const arr = []
                cat.heroerList.forEach(hero => {
                    const index = heroesList.findIndex(v => v.name === hero.name)
                    if (index > -1) {
                        heroesList[index] = {
                            ...heroesList[index],
                            categories: [...heroesList[index].categories, category._id]
                        }
                    } else {
                        arr.push({
                            name: hero.name,
                            avatar: hero.icon,
                            categories: [category._id]
                        })
                    }
                })
                heroesList = [...heroesList, ...arr]
            }
        }
        const data = await Hero.insertMany(heroesList)
        res.send(data)
    })

    // 获取英雄列表
    router.get("/heroes/list", async (req, res) => {
        /**
         * const parent = await Category.findOne({ name: "英雄分类" })
         * const conditions = { parent: parent._id } // 查询条件
         * const populate = { path: "heroes" } // 关联参数
         * const options = { lean: true } // 查询参数
         * const result = await Category.find(conditions, null, options).populate(populate)
         */
        
        const parent = await Category.findOne({ name: "英雄分类" })
        const cats = await Category.aggregate([
            { $match: { parent: parent._id } },
            {
                $lookup: {
                    from: "heroes",
                    localField: "_id",
                    foreignField: "categories",
                    as: "heroList"
                }
            },
            {
                $addFields: {
                    heroList: { $slice: ["$heroList", 100] }
                }
            }
        ])

        // 添加热门分类
        const subCats = cats.map(v => v._id)
        cats.unshift({
            name: "热门",
            heroList: await Hero.find({ categories: { $in: subCats } })
            .populate("categories").limit(20).lean()
        })

        cats.map(cat => {
            cat.heroList.map(hero => {
                hero.categoryName = cat.name === "热门" ? hero.categories[0].name : cat.name
                return hero
            })
            return cat
        })

        res.json({
            code: 1,
            data: cats
        })
    })

    // 获取英雄详情
    router.get("/heroes/:id", async (req, res) => {
        // const populate = [
        //     { path: "categories", select: "name -_id" },
        //     { path: "items1", select: "name icon -_id" },
        //     { path: "items2", select: "name icon -_id" },
        //     { path: "partners.hero", model: "Hero", select: "name avatar -_id" }
        // ]
        const { id } = req.params
        const result = await Hero.findById(id).populate('categories items1 items2 partners.hero').lean()
        result.jobs = result.categories.map(v => v.name).join(',')
        res.json({
            code: 1,
            data: result
        })
    })

    app.use("/web/api", router)
}